home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / pcfilerd.arc / PCFRD.DOC < prev   
Encoding:
Text File  |  1988-09-20  |  5.6 KB  |  94 lines

  1.    The text below is from The C Gazette, Summer 1988, Volume 3
  2. No. 1, and is the magazine text for PCFILERD.C.
  3.  
  4. ----------------------------------------------------------------------------
  5.    PC-FILE and it's successor PC-FILE+ are the most widely distributed data-
  6. base managers for the PC. Their success is due to their ease of use and their 
  7. availability as shareware. This article discusses the file formats and access 
  8. to the data and index records using C.
  9.  
  10.    PC-FILE+ provides programmers with an inexpensive front-end to database
  11. applications. The programs that handle data-entry and record management are 
  12. commonly available as shareware. They are ideal for the management of small 
  13. databases. Few packages are as easy to install and use, and none offers so 
  14. much functionality at such a low price. The complete system with a bound 
  15. manual and technical support is available for $69.95 from Buttonware (ad-
  16. dress at end of article).
  17.    PC-FILE manages data as a flat file that is a collection of fixed-length 
  18. sequential records. A separate index file permits random access to individual 
  19. data records.
  20.    The index file contains one record for each data record. Every field in
  21. the data record has a corresponding two-byte index in the index record, such 
  22. that every field of every data record is accessible directly. The index field 
  23. contains the first two bytes of the data field, such that a name field cont-
  24. aining "ERIC", for example, would have an index entry of "ER". The index 
  25. record contains two bytes for each data field plus a two-byte unsigned integer 
  26. at the end which holds the record number of the data. This number is limited 
  27. to the range of an unsigned int, 65536, and predictably, this is the upper 
  28. limit on the number of records supported in one database. (Early versions used 
  29. a signed integer and were limited thereby to half the current capacity.)
  30.    To find a data record based on the record number, the following formula
  31. is to calculate the record's offset into the data file:
  32.       (record number - 1) * length of data
  33.    The record number should be decremented because the first record (number
  34. 1) starts at an offset of zero bytes into the file. The length of the data 
  35. record is the sum of all the data fields plus one byte. This byte is a 
  36. carriage return (0x0d) which PC-FILE appends to each data record.
  37.    To access a database, we need to know the number of fields in each record
  38. (to know the length of the index record) and how many bytes of data each 
  39. record holds (to know the length of the data records). This information is 
  40. available in a separate header file maintained by PC-FILE.
  41.    Every database contains at least three files: filename.inx, filename.dta,
  42. and filename.hdr. These are the index, data, and header files respectively. 
  43. The header file is a straight ASCII file which lists each data field: it's 
  44. name, size, and placement in the record. The keys in the index file follow the 
  45. same order as the data record layout.
  46.    Access to a data file is done as follows. Suppose a mailing list file
  47. were being queried for all persons living in Dubuque. The index file would be 
  48. read one record at a time and the city key would be checked against the first 
  49. two bytes of the search key, namely "DU". When a match occurs, the data record 
  50. is read (based on calculation the offset into the data file) and the actual 
  51. city name is verified. This check avoids denizens of DUluth from being 
  52. accepted by the query.
  53.    A sequential read of the file requires reading each index record and look-
  54. ing up the data record. The accompanying program, PCFILERD.C, demonstrates the 
  55. technique of a sequential read through a file.
  56.    Two special cases need to be accounted for. Deleted records are flagged
  57. by the placement of a forward slash in the first byte of the index record. 
  58. Such records should simply be skipped. End-of-file is indicated by a backslash 
  59. in the first byte. This should always be checked for and considered a true EOF 
  60. condition. PC-FILE maintains one record at file's end with this flag; hence a 
  61. sequential read will never attempt to go beyond the end-of-file. The function 
  62. get_record() illustrates the handling of these special cases.
  63.    ( Typist's note: The accompanying program displays my database. The func-
  64. tion print_line() as it appears in the magazine article follows below. If you 
  65. have trouble printing your database, try replacing the print_line() in the 
  66. accompanying program PCFILERD.C with this version: )
  67.  
  68.  
  69. void print_line()       /* Just a sample application: print the first */
  70. {                       /*     forty characters of the data record */
  71.  
  72.    char line [40];
  73.  
  74.    strncpy (line, Data_buffer, 40);
  75.    if (strlen (line) > 39) /* Remember, strncpy() may not end */
  76.       line[39] = '\0';     /*    the string with a null.      */
  77.    printf ("%s\n", line);
  78. }
  79.  
  80.    This application is simply for demonstration, since all forms of data
  81. manipulation are of course possible.
  82.    The information in this article relies in no small part on material pre-
  83. sented in the manual to PC-FILE+. (Typist's note: I used this program with
  84. PC-FILE III Ver.4 and it works fine.)  This book should be bought by anyone 
  85. who wishes to use PC-FILE+ to it's full effect. An appendix contains some 
  86. sample C source code which demonstrates file access. However, it would be kind 
  87. to say that the author's native language is not C. The coding techniques 
  88. should not be imitated at all, and the routines presented here should be used 
  89. instead.
  90.    PC-FILE+ is sold by Buttonware, P.O. Box 5786, Bellevue, WA 98006.
  91. (800) JBUTTON. Not surprisingly, PC-FILE and PC-FILE+ are trademarks of 
  92. Buttonware.
  93.  
  94.